const express = require('express')
const app = express()
var Mock = require('mockjs');
var Random = Mock.Random;
// var cors = require("cors")

// var one = require('./src/router/one');

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));

app.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    //Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    res.header('Access-Control-Allow-Methods', '*');
    res.header('Content-Type', 'application/json;charset=utf-8');
    next();
});

app.get('/', (req, res) => res.send('Hello World!'))

app.post('/login', function (req, res) {

    var userName = req.body['userName'] || '';
    var pwd = req.body['pwd'] || '';

    if (userName.trim() == 'admin' && pwd == '888888') {
        res.send({
            Result: true,
            ResultCode: 1000,
            ResultMsg: '操作成功',
            ResultData: null
        })
        return;
    }

    res.send({
        Result: false,
        ResultCode: 0,
        ResultMsg: '帐号或密码错误',
        ResultData: null
    })



});

app.get('/getUserList', function (req, res) {
    var data = Mock.mock({
        'list|20': [{
            'id|+1': 1,
            'serial_number|1-100': 1,
            'warn_number|1-100': 1,
            'warn_name|1': ['流水线编排服务异常', '磁盘占用超过阈值'],
            'warn_level|1': ['紧急', '重要'],
            'warn_detail': '环境IP:10.114.123.12,服务名称:XX',
            'create_time': Random.datetime(),
            'finish_time': Random.datetime(),
            'contact|4': 'abc'
        }]
    });
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.send({
        Result: true,
        ResultCode: 1000,
        ResultMsg: '操作成功',
        ResultData: data
    })

});
// app.use(cors({credentials: true, origin: 'http://localhost:3000'}));

app.listen(3000, () => console.log('Example app listening on port 3000!'))